/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;
import data.KeahlianData;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author Muhammad Dony
 */
public class KeahlianDB extends ConnectDB {

    private String sql;
    private Connection con;
    private Statement stmt;
    private ResultSet rs;
    
    public List getkeahlian() throws SQLException, ClassNotFoundException{
        sql = "SELECT * from `keahlian`";
        return getkeahliandata(sql);
    }
    
    public List getkeahlianById(String ID) throws SQLException, ClassNotFoundException {
        sql = "SELECT * from `keahlian` WHERE `ID`='"+ID+"';";
        return getkeahliandata(sql);
    }
    
    public List getkeahlianByxx(String ID, int no) throws SQLException, ClassNotFoundException {
        sql = "SELECT * from `keahlian` WHERE `ID`='"+ID+"' AND `nomor`='"+no+"';";
        return getkeahliandata(sql);
    }
        
     private List getkeahliandata(String sql) throws SQLException, ClassNotFoundException{
        // mempersiapkan list dataPelaporan
        List listkeahlian = new ArrayList();
        //membuat koneksi , createConnection terdapat dalam DBClass
        con = createConnection();
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        // mempersiapkan object dataPelaporan
        KeahlianData keahlianUser;
        
        while(rs.next()){
            keahlianUser = new KeahlianData();
            keahlianUser.setNomor(rs.getInt(1));
            keahlianUser.setID(rs.getString(2));
            keahlianUser.setBidang(rs.getString(3));
            keahlianUser.setJenis(rs.getString(4));
            keahlianUser.setTingkat(rs.getString(5));
            listkeahlian.add(keahlianUser);
        }
        // menutup semua koneksi db
        rs.close();
        stmt.close();
        con.close();
        //return value list data pelaporan
        return listkeahlian;
        
    }
    
    private void actkeahlian(String sql) throws ClassNotFoundException, SQLException{
        // membuat koneksi
        con = createConnection();
        stmt = con.createStatement();
        // execute query
        stmt.execute(sql);
        // menutup koneksi db
        stmt.close();
        con.close();
    }
   public void insertKeahlian(KeahlianData listKeahlian) throws ClassNotFoundException, SQLException{
        // query
        sql = "INSERT INTO `scholarship`.`keahlian` (`ID`, `bidang`, `jenis`, `tingkat`) VALUES ('"
                +listKeahlian.getID()+"', '"
                +listKeahlian.getBidang()+"', '"
                +listKeahlian.getJenis()+"', '"
                +listKeahlian.getTingkat()+"');";
        actkeahlian(sql);
    }
   

   //// EDIT DAN DELETE MASIH BERMASALAH Y!!!!!
   
   
    public void updateKeahlian(KeahlianData listKeahlian) throws ClassNotFoundException, SQLException{
        // query
        sql = "UPDATE `keahlian` SET `bidang`='"+listKeahlian.getBidang()+
                "',`jenis`='"+listKeahlian.getJenis()+
                "',`tingkat`='"+listKeahlian.getTingkat()
                +"' WHERE `ID`='"+listKeahlian.getID()+"' AND nomor="+listKeahlian.getNomor()+";";
        // memanggil method untuk execute query
        actkeahlian(sql);
    }
    public void deleteKeahlian(String ID, int keahlian) throws ClassNotFoundException, SQLException{
        sql = "DELETE FROM `keahlian` WHERE `ID`='"+ID+"' AND `nomor`='"+keahlian+"';";
        actkeahlian(sql);
    } 
    
    
    
}
